package cn.dnuihyy406.dnuifood_16110100406.controller.foodlist;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.dnuihyy406.dnuifood_16110100406.R;
import cn.dnuihyy406.dnuifood_16110100406.bean.CollectStatus;
import cn.dnuihyy406.dnuifood_16110100406.bean.FoodBean;
import cn.dnuihyy406.dnuifood_16110100406.bean.ResultBean;
import cn.dnuihyy406.dnuifood_16110100406.listener.OnRetrofitListener;
import cn.dnuihyy406.dnuifood_16110100406.model.FoodModel;

public class FoodListActivity extends AppCompatActivity implements OnRetrofitListener<Object> {
    private RecyclerView recyclerView;
    private FoodListAdapter foodListAdapter;
    private FoodBean foodbean;
    String shop_id,shop_name;
    private String user_id;
    List<FoodBean>list;
    SharedPreferences sharedPreferences;
    private ImageView Xing;
    private TextView text_title;
    String result="0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState);
        setContentView(R.layout.activity_food_list);
        Xing=findViewById(R.id.xingxing);
        sharedPreferences = getSharedPreferences("hyy", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getString("userid","");
        shop_id = getIntent().getStringExtra("shop_id");
        shop_name=getIntent().getStringExtra("shopname");
//        Toast.makeText(FoodListActivity.this,"欢迎光临"+shop_name,Toast.LENGTH_SHORT).show();
        getFoodList(shop_id);
        initViews();
        initData();
        initEvents();
    }





    @Override
    public void onSuccess(Object object, int flag) {
        switch (flag){
            case 2:
                //获取店铺的菜谱列表
                foodListAdapter = new FoodListAdapter(FoodListActivity.this, (List) object ,R.layout.food_list_item);
                recyclerView.setAdapter(foodListAdapter);
                break;
            case 3:
                //获取店铺的收藏状态
                result = ((CollectStatus)object).getCollected();
                if(result.equals("0"))
                { Xing.setImageResource(android.R.drawable.btn_star_big_off); }
                else { Xing.setImageResource(android.R.drawable.btn_star_big_on); }
                break;
            case 4:
                //对店铺的收藏信息进行修改
                if(((ResultBean)object).getSuccess().equals("1")){
                    if(result.equals("0")){
                        Xing.setImageResource(android.R.drawable.btn_star_big_on);
                        Toast.makeText(FoodListActivity.this,"店铺收藏成功",Toast.LENGTH_SHORT).show();
                        result="1";
                    }
                    else {
                        Xing.setImageResource(android.R.drawable.btn_star_big_off);
                        Toast.makeText(FoodListActivity.this,"取消店铺收藏成功",Toast.LENGTH_SHORT).show();
                        result="0";
                    }
                }
                else {
                    Toast.makeText(FoodListActivity.this,"操作失败",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(FoodListActivity.this,"网络连接失败",Toast.LENGTH_LONG).show();
    }

    void initViews(){
        recyclerView = this.findViewById(R.id.RecylerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }
    void initData(){
//        ShopModel shopModel = new ShopModel();
//        shopModel.getFoodByShop(this);
        iscollect(user_id);
    }
    void initEvents(){
        Xing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                collect();
            }
        });

    }
    private List<FoodBean> getData() {
        List<FoodBean> list = new ArrayList() ;
        return list;
    }

    private void getFoodList(String shop_id) {
        FoodModel foodModel = new FoodModel();
        foodModel.getFoodByShop(shop_id,this);
    }
    //发送获取店铺的收藏状态的网络请求
    private void iscollect(String user_id) {
        FoodModel foodModel = new FoodModel();
        foodModel.isCollected(user_id,shop_id,"0",this);
    }
    //发送对店铺的收藏信息进行修改的网络请求
    private void collect(){
        FoodModel foodModel = new FoodModel();
        foodModel.userCollectShop(user_id,shop_id,this);
    }
}